<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
		var aJaxURL		= "server-side/documents/buying_act.action.php";				//server side folder url
		var aJaxURL_in	= "server-side/documents/buying_act/in.action.php";				//server side folder url
		var seoyURL		= "server-side/seoy/seoy.action.php";							//server side folder url
		var fName		= "add-edit-form";												//form name
		var tbName		= "tabs";														//tabs name
		
		$(document).ready(function() {
			ClearDB();
			GetTabs(tbName);
			GetIN();
		});
		
		//SeoYyy
		$(document.body).click(function(e) {
            $("#seller_tin").autocomplete("close");
            $("#seller_name").autocomplete("close");
            $("#goods_code").autocomplete("close");
            $("#goods_name").autocomplete("close");
		});
		
		function GetIN() {
			LoadTable("in-overhead");
			GetButtons("add_button_in", "", "export_button_in", "");
			SetEvents("add_button_in", "delete_button_in", "check-all-in", "in-overhead", fName, aJaxURL);
		}
		
		function LoadTable(tName) {
			if (tName == "in-overhead") {
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable(tName, aJaxURL, "get_list", 5, "", 0, "", 0, "desc");                	
			} else {				
				var menuLength	= [[10], [10]];
				var id			= $("#buying_id").val();
				var local_id	= $("#local_buying_id").val();
				
				if (!empty(id))					
					local_id = id;
				
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable("goods_list", aJaxURL_in, "get_list", 8, "local_id=" + local_id, 0, menuLength, 1, "asc");
			}
		}
		
		function LoadDialog(form) {
			if (form == fName) {  //Main Overhead Page
				ClearDB();
				var buttons = {
					"active": {
						text: "მიღება",
						id: "keep-buying",
						click: function() {
						}
					}
				};
				GetDialog(fName, 950, "auto", buttons, "");
				GetButtons("add_button_goods", "delete_button_goods", "export_button_goods");
				
	            SeoY("seller_tin", seoyURL, "overhead_buyer_tin", "", 1);
	            SeoY("seller_name", seoyURL, "overhead_buyer_name", "", 0);
	                        
	            var id		= $("#buying_id").val();
	            if (empty(id)) {
	            	var payed = $("#payed").val();
	                if(empty(payed)){
	                	$("#payed").val(0);
					}
	            	var local_id = GetLocalID();
	            	$("#local_buying_id").val(local_id);
	            	GetDate("operation_data");
	            	GetDate("pay_needed_date");
	            	LoadTable("goods_list");
	            	SetEvents("add_button_goods", "delete_button_goods", "check-all-goods", "goods_list", "add-edit-goods-form", aJaxURL_in);
	            }else{
	            	var buttons = {
	            			"cancel": {
	            	            text: "დახურვა",
	            	            id: "cancel-dialog",
	            	            click: function () {
	            	                $(this).dialog("close");
	            	            }
	            	        }
                        };
                        
					$("#" + fName).dialog("option", "buttons", buttons);
	            	DisableHeader();
	            	DisableRelations();
	            	DisableAccounts();
	            	DisableGoods();
	            	LoadTable("goods_list");	            	
		        }
			} else {  //Goods Page
            	var object	= $("#object").val();
                var id		= $("#goods_id").val();
            	
                SeoY("goods_code", seoyURL, "buyng_goods_code", "", 1);
                SeoY("goods_name", seoyURL, "buyng_goods_name", "", 0);
                
                if (id != "") {
                    $("#goods_quantity").focus();
                }
                var buttons = {
                    "save": {
                        text: "დამატება",
                        id: "add-goods",
                        click: function() {
                        }
                    }
                };
                /* Dialog Form Selector Name, Buttons Array */
                GetDialog("add-edit-goods-form", 470, "auto", buttons);
            }
		}
		
		// Add Buying Act
        $(document).on("click", "#keep-buying", function() {
            var act_id	= $("#local_buying_id").val();
        	var check	= CheckGoodsList(act_id);
        	
        	if(check){
	            param = new Object();
	
	            //Action
	            param.act	= "save_buying_act";
	            param.stat	= 1; //status active
	            param.obj	= $("#object").val();
	            param.lid	= act_id;
	            
	            
	            param.id	= $("#buying_id").val();
	            
	            //Main
	            param.dn	= $("#d_number").val().trim();
	            param.ad	= $("#operation_data").val().trim();
	            
	            param.st	= $("#seller_tin").val().trim();
	            param.sn	= $("#seller_name").val().trim();
	            
	            if (param.dn == "") {
	                alert("შეავსეთ დოკუმენტის №!");
	            } else if (param.ad == "") {
	                alert("შეავსეთ ოპერაციის თარიღი!");
	            } else if (param.st == 0) {
	                alert("შეავსეთ საიდენტ. ნომერი!");
	            } else if (param.sn == "") {
	                alert("შეავსეთ გამყიდველს დასახელება / სახელი, გვარი!");
	            } else {
	                $.ajax({
	                    url: aJaxURL,
	                    data: param,
	                    success: function(data) {
	                        if (typeof(data.error) != "undefined") {
	                            if (data.error != "") {
	                                alert(data.error);
	                            } else {
	                                AddPartnerAccount(data.buying_act_id); //Partner account
	                                LoadTable("in-overhead");
	                                CloseDialog(fName);
	                            }
	                        }
	                	}
	            	});
	            }
        	}
        });
		
		// Add Goods
        $(document).on("click", "#add-goods", function() {
            param = new Object();
            
            //Action
            param.act	= "save_prod";
            param.obj	= $("#object").val();
            
            param.id	= $("#goods_id").val();
            param.li	= GetLocalID();
            
            //Main
            param.gc	= $("#goods_code").val().trim();
            param.gn	= $("#goods_name").val().trim();
            param.gm	= $("#goods_measure").val();
            param.gq	= $("#goods_quantity").val().trim();
            param.gu	= $("#goods_unit_price").val().trim();
            param.gp	= $("#goods_price").val().trim();
            param.gv	= $("#goods_vat").val();         
            
            if (param.gn == "") {
                alert("შეავსეთ საქონლის დასახელება!");
            } else if (param.gm == 0) {
                alert("შეავსეთ საქონლის ზომის ერთეული!");
            } else if (param.gq == "" || param.gq <= 0) {
                alert("შეავსეთ საქონლის რაოდენობა!");
            } else if (param.gu == "" || param.gu <= 0) {
                alert("შეავსეთ საქონლის ერთეულის ფასი!");
            } else if (param.gp == "" || param.gp <= 0) {
                alert("შეავსეთ საქონლის ფასი!");
            } else {
                $.ajax({
                    url: aJaxURL_in,
                    data: param,
                    success: function(data) {
                        if (typeof(data.error) != "undefined") {
                            if (data.error != "") {
                                alert(data.error);
                            } else {
                                LoadTable("goods_list");
                                CloseDialog("add-edit-goods-form");
                                CalculateAmount(param.li);
                            }
                        }
                	}
            	});
            }
        });

        function CheckGoodsList(act_id) {
            var check = false;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=check_goods_list&id=" + act_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
            	}
        	});
            return check;
        }
        
        function AddPartnerAccount(act_id) {
        	param = new Object();
        	
            //Action
            param.act	= "add_partner_acc";
            
            param.id  	= act_id;                
            param.bin	= $("#seller_tin").val();	//Partner ID
            param.pm	= $("#pay_method").val();
            param.pn	= $("#pay_nided").val();
            param.p		= $("#payed").val();
            param.pnd	= $("#pay_needed_date").val();
            param.type	= 2;
            
            $.ajax({
                url: aJaxURL,
                data: param,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
            	}
        	});
        }
        
        /**
         * Get next auto incremented id
         * @return {int}   Returns a next id
         */
        function GetLocalID() {
            var local_id;
            $.ajax({
                url: aJaxURL,
                async: false, //r-value
                data: "act=get_local_id",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            local_id = data.local_id;
                        }
                    }
                }
            });
            return local_id;
        }
        
        /**
         * Set inputs Buyer Name & Buyer Tin
         * @param {int} tin, {string} name,
         */
        function SetBuyer(tin, name) {
            var url = aJaxURL;
            $.ajax({
                url: url,
                data: "act=get_seller&tn=" + tin + "&nm=" + name,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            $("#seller_tin").val(data.tin);
                            $("#seller_name").val(data.name);
                        }
                    }
                }
            });
        }
        
        /**
         * Set inputs Goods Name & Barcode
         * @param {int} barcode, {string} name,
         */
        function SetGoods(barcode, name) {
            $.ajax({
                url: aJaxURL_in,
                data: "act=get_goods&bc=" + barcode + "&nm=" + name,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            $("#goods_code").val(data.code);
                            $("#goods_name").val(data.name);
                            $("#goods_measure").val(data.measure);
                            $("#goods_unit_price").val("");
                            $("#goods_quantity").val("");
                            $("#goods_quantity").focus();
                        }
                    }
                }
            });
        }

        function CalculateAmount(act_id){
            $.ajax({
                url: aJaxURL,
                data: "act=get_amount&id=" + act_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            $("#pay_nided").val(data.amount);
                        }
                    }
                }
            });
        }
        
        //Buyer Effects
        $(document).on("keydown", "#seller_tin", function(event) {
            if (event.keyCode == $.ui.keyCode.ENTER) {
                SetBuyer(this.value, "");
            }
        });
        
        $(document).on("keydown", "#seller_name", function(event) {
            if (event.keyCode == $.ui.keyCode.ENTER) {
                SetBuyer("", this.value);
            }
        });
        //--
        
        //--Goods Out Calculations
        $(document).on("keydown", "#goods_code", function(event) {
            if (event.keyCode == $.ui.keyCode.ENTER) {
                SetGoods(this.value, "");
                event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
        });
        
        $(document).on("keydown", "#goods_name", function(event) {
            if (event.keyCode == $.ui.keyCode.ENTER) {
                SetGoods("", this.value);
                event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
        });
        
        $(document).on("keyup", "#goods_quantity", function(event) {
        	var quantity = $("#goods_quantity").val();
            if (event.keyCode == $.ui.keyCode.ENTER && !empty(quantity)) {
                $("#goods_unit_price").focus();
                event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
        });
        
        $(document).on("keyup", "#goods_unit_price", function(event) {
            var gq = $("#goods_quantity").val().trim();
            var gup = $("#goods_unit_price").val().trim();

            $("#goods_price").val((gq * gup).toFixed(4));

            if (event.keyCode == $.ui.keyCode.ENTER) {
            	$("#add-goods").focus();
                event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
        });       
        //--
                
        //--SeoY
        $(document).on("click", ".combobox", function(event) {
            var i = $(this).text();
            $("#" + i).autocomplete("search", "");
        });
                
        function ClearDB() {
            $.ajax({
                url: aJaxURL,
                data: "act=clear_db",
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        }
                    }
                }
            });
        }
        
        function DisableHeader() {
            $("#d_number").attr("disabled", "disabled");
            $("#operation_data").attr("disabled", "disabled");
            $("#object").attr("disabled", "disabled");
        }
        
        function DisableRelations() {
            $("#seller_tin").attr("disabled", "disabled");
            $("#seller_name").attr("disabled", "disabled");
            $("#seller_name_btn").button("disable");
        }
        
        function DisableAccounts() {
            $("#pay_method").attr("disabled", "disabled");
            $("#pay_nided").attr("disabled", "disabled");
            $("#payed").attr("disabled", "disabled");
            $("#pay_needed_date").attr("disabled", "disabled");
        }
        
        function DisableGoods() {
            $("#add_button_goods").button("disable");
            $("#delete_button_goods").button("disable");
        }
	</script>
</head>
<body>
	<div id="tabs" style="width: 1024px; margin: 0 auto; min-height: 768px;">
		<ul>
			<li>
				<a href="#tab-in">მიღებული</a>
			</li>
		</ul>
		<div id="tab-in">
			<div id="dt_example" class="ex_highlight_row">
				<div id="container" class="overhead_container">
					
					<div id="dynamic">
						<h2>შეძენის აქტი</h2>
						<div id="button_area">
							<button id="add_button_in">ახალი დოკუმენტი</button>
						</div>
						<table class="display" id="in-overhead">
							<thead>
								<tr id="datatable_header">
									<th>ID</th>
									<th class="min">დოკუმენტის №</th>
									<th style="width: 514px">პარტნიორი</th>
									<th class="min">თანხა</th>
									<th class="min" title="ოპერაციის თარიღი">ოპერაციის თარიღი</th>
								</tr>
							</thead>
							<thead>
								<tr class="search_header">
									<th class="colum_hidden"><input type="text" name="search_id" value="ფილტრი" class="search_init" />									
									</th>
									<th><input type="text" name="search_overhead" value="ფილტრი" class="search_init" />
									</th>
									<th><input type="text" name="search_partner" value="ფილტრი" class="search_init" />
									</th>
									<th><input type="text" name="search_op_date" value="ფილტრი" class="search_init" />
									</th>
									<th><input type="text" name="search_sum_cost" value="ფილტრი" class="search_init" />
									</th>
								</tr>
							</thead>
						</table>
					</div>
					<div class="spacer"></div>
				</div>
			</div>
		</div>
	</div>
	<!-- jQuery Dialog -->
	<div id="add-edit-form" class="form-dialog" title="შეძენის აქტი">
		<!-- aJax -->
	</div>
</body>
</html>